<template>
  <el-dialog
    @close="cancel"
    :title="title"
    :visible.sync="open"
    width="600px"
    class="dialogClass"
    append-to-body
  >
    <template slot="title">
      <div class="modalTitle">
        <img
          src="../../../../assets/bluelogo/drawerTitle.png"
          alt=""
          class="titleLogo"
        />{{ title }}
      </div>
    </template>
    <el-form
      ref="form"
      v-if="this.modalType == 'edit' || this.modalType == 'add'"
      :model="formData"
      :rules="rules"
      label-width="110px"
    >
      <el-form-item label="角色名称:" prop="roleId">
        <el-select
          v-model="formData.roleId"
          placeholder="请选择角色名称"
          clearable
        >
          <el-option
            v-for="item in roleOptions"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处置类别:" prop="limitType">
        <el-select
          v-model="formData.limitType"
          placeholder="请选择处置类别"
          clearable
        >
          <el-option
            v-for="dict in dict.type.limit_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          /> </el-select
      ></el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="时限类型:" prop="timeType">
            <el-select
              v-model="formData.timeType"
              placeholder="请选择处置时限时间类型"
              clearable
            >
              <el-option
                v-for="dict in dict.type.time_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="处置时限:" prop="timeLimit">
            <el-input
              v-model="formData.timeLimit"
              :disabled="!formData.timeType"
              placeholder="请输入处置时限"
              maxlength="3"
              oninput="value=value.replace(/[^0-9.]/g,'')"
              clearable
            ></el-input> </el-form-item></el-col
      ></el-row>
    </el-form>
    <div slot="footer">
      <div class="dialog-footer">
        <el-button @click="cancel" class="resetButton">{{
          resetButtonContent
        }}</el-button>
        <el-button class="searchButton" @click="submitForm">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { MODALTYPE } from "../../../../utils/variable";
//案件重复校验

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getUser } from "@/api/system/user";
import {
  delTime,
  getTime,
  addTime,
  updateTime,
  timeLimitValidate,
} from "@/api/timeLimit/time";
export default {
  inheritAttrs: false,
  dicts: ["management_type", "limit_type", "time_type"],
  components: { Treeselect },
  props: ["modalType", "id"],

  data() {
    var checkLimitType = (rule, value, callback) => {
      if (!value) {
        callback(new Error("处置类别不能为空"));
      } else if (this.modalType == MODALTYPE["新增"]) {
        let params = {
          roleId: this.formData.roleId,
          limitType: this.formData.limitType,
        };
        timeLimitValidate(params).then((response) => {
          if (response.data === 1) {
            callback(new Error("在该角色下处置类别已重复！"));
          } else {
            callback();
          }
        });
      } else if (this.modalType == MODALTYPE["修改"]) {
        if (
          this.temCaseName !== this.formData.roleId &&
          this.temRulingBookNumber !== this.formData.limitType
        ) {
          let params = {
            roleId: this.formData.roleId,
            limitType: this.formData.limitType,
          };
          timeLimitValidate(params).then((response) => {
            if (response.data === 1) {
              callback(new Error("在该角色下处置类别已重复！"));
            } else {
              callback();
            }
          });
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      isDisabled: false,
      // 弹出层标题
      title: "",
      // 角色选项
      roleOptions: [],
      temCaseName: undefined,
      temRulingBookNumber: undefined,
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      formData: {
        roleName: undefined,
        managementType: undefined,
        timeType: undefined,
        timeLimit: undefined,
      },
      rules: {
        roleId: [
          {
            required: true,
            message: "请输入角色名称",
            trigger: "blur",
          },
        ],
        limitType: [
          {
            required: true,
            validator: checkLimitType,
          },
        ],
        timeType: [
          {
            required: true,
            message: "请输入处置时限时间类型",
            trigger: "blur",
          },
        ],
        timeLimit: [
          {
            required: true,
            message: "请输入处置时限",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    resetButtonContent() {
      if (this.modalType == "detail") {
        return "关闭";
      } else {
        return "取消";
      }
    },
  },
  watch: {
    modalType: {
      handler(value) {
        if (value == "add") {
          this.reset();
          getUser().then((response) => {
            this.roleOptions = response.roles;
          });
          this.open = true;
          this.title = "新增处置时限";
        } else if (value == "edit") {
          this.reset();
          getUser().then((response) => {
            this.roleOptions = response.roles;
          });
          if (this.id) {
            getTime(this.id).then((response) => {
              this.formData = response.data;
              this.temCaseName = this.formData.roleId;
              this.temRulingBookNumber = this.formData.limitType;
              this.open = true;
              this.title = "修改处置时限";
            });
          }
        }
        // else if (value == "detail") {
        //   this.isDisabled = true;
        //   this.reset();
        //   if (this.id) {
        //     getCase(this.id).then((response) => {
        //       this.formData = response.data;
        //       this.open = true;
        //       this.title = "处置时限详情";
        //     });
        //   }
        // }
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    // 取消按钮
    cancel() {
      this.$emit("update:modalType", undefined);
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        caseName: undefined,
        rulingBookNumber: undefined,
        executeNumber: undefined,
        caseState: undefined,
        bm: undefined,
        involvedPeople: undefined,
        payedPeople: undefined,
        payedMoney: undefined,
        needMoney: undefined,
        introduction: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.modalType == "edit") {
            this.formData.id = this.id;
            updateTime(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("update:modalType", undefined);
              this.$emit("fatherMethod");
            });
          } else if (this.modalType == "add") {
            addTime(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("update:modalType", undefined);
              this.$emit("fatherMethod");
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../../../assets/styles/dialog.scss"; //模态框样式

::v-deep .label {
  font-size: 18px !important;
  font-family: Microsoft YaHei !important;
  font-weight: 400 !important;
  color: #598af9 !important;
  margin-top: 15px;
}
::v-deep.el-select {
  width: 100%;
}
::v-deep .content {
  font-size: 16px;
  margin-left: 30px;
  margin-top: 15px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #aeafb0;
}
::v-deep .el-descriptions-item__container {
  margin-bottom: 20px;
}
</style>
